package massageBox;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.util.Log;


public class Sender implements ISender{
	
	
	public Map<String, Object> sendWindow(String room, int position){
		

		String[] myValues = {String.valueOf(position),"SLOW"};
		
		JSONArray myJSONArray = new JSONArray(Arrays.asList(myValues));
		
		//build change Map
		Map<String, Object> changeMap = new HashMap<String, Object>();
		changeMap.put(room, myJSONArray);
		
		send(changeMap,"WINDOW.CONTROL");
		
		return changeMap;
		
	}

	
	public Map<String, Object> sendLight(String room, boolean status){
		
		Log.i("info","Sender.sendLight open with room "+room.toString()+" and status "+status);
		
		//set the values for lightOn or lightOff
		Map<String, String> valueMap = new HashMap<String, String>();
		if (status){
			valueMap.put("red", "255");
			valueMap.put("green", "255");
			valueMap.put("blue", "255");
			valueMap.put("fadeTime", "3");
		}
		else{
			valueMap.put("red", "0");
			valueMap.put("green", "0");
			valueMap.put("blue", "0");
			valueMap.put("fadeTime", "3");
		}
		
		
		//set the room light parameter for the given attribut
		String myRoom = null;
		if (room.equals("SLEEPING")){
			Log.i("info","SLEEPING - was detected as room");
			myRoom = "sleeping_light_color";
		}	
		else if(room.equals("LIVING")){
			Log.i("info","LIVING - was detected as room");
			myRoom = "lounge_light_color";
			//myRoom = "corridor_light_color"; 
		}
		else if(room.equals("DINING")){
			Log.i("info","DINING - was detected as room");
			myRoom = "dining_light_color";
		}
		else if(room.equals("KITCHEN")){
			Log.i("info","KITCHEN - was detected as room");
			myRoom = "kitchen_main_light_color";
		}
		
		//build change Map
		Map<String, Object> changeMap = new HashMap<String, Object>();
		changeMap.put("values", new JSONObject(valueMap));
		changeMap.put("Id", "client_1578909153");
		changeMap.put("Version", null);
		changeMap.put("action", myRoom);
		
		//send(changeMap,"LP.LIGHTCONTROL");
		
		return changeMap;

	}
	
	
	
	public String send(Map<String, Object> changeMap,String control) {

		JSONObject msgJSON = new JSONObject(changeMap);

		new sendMessageToProxy().execute(new String[] { "172.16.0.200",
				"12349",  control, "topic",
				msgJSON.toString() });
		
		Log.i("msgJason: ", msgJSON.toString());
		return msgJSON.toString();

	}
	
	
	public String sendOld() {
		
		Log.i("Info","sendOld activate");

		Map<String, String> valueMap = new HashMap<String, String>();
		valueMap.put("red", "255");
		valueMap.put("green", "0");
		valueMap.put("blue", "128");
		valueMap.put("fadeTime", "0");

		Map<String, Object> aendernMap = new HashMap<String, Object>();
		aendernMap.put("action", "dining_light_color");
		aendernMap.put("values", new JSONObject(valueMap));
		aendernMap.put("Id", "client_1578909153");
		aendernMap.put("Version", null);
		

		JSONObject msgFarbeEsszimmerAendern = new JSONObject(aendernMap);
		// myMsg.setType("");

		new sendMessageToProxy().execute(new String[] { "172.16.0.200",
				"12349", "LP.LIGHTCONTROL","topic", 
				msgFarbeEsszimmerAendern.toString() });
		return msgFarbeEsszimmerAendern.toString();

	}

	private class sendMessageToProxy extends AsyncTask<String, Void, String> {

		/**
		 * Creates instance of AndroidPublisher publisher and publish the
		 * massage to queue or topic
		 * 
		 * @param params
		 *            [0] Server; [1] Port; [2] TopicName; [3] topic/queue; [4]
		 *            massage;
		 * */
		@Override
		protected String doInBackground(String... params) {
			try {
				AndroidPublisher publisher = new AndroidPublisher(params[0],
						Integer.valueOf(params[1]), params[2]);

				publisher.setMessage(params[4]);
				if (params[3].equals("topic")) {
					publisher.publishToTopic();
				} else {
					publisher.publishToQueue();
				}

			} catch (IOException e) {
				Log.e("Publisher", "Can't publish the message");
			}
			return null;
		}

	}

}
